package com.heaboy.collection;

public class lianbiao {
    public static void main(String[] args) {
        Node node=new Node(1);
        Node node1=new Node(2);
        linkedlist linkedlist=new linkedlist();
        //正常插入节点
//      linkedlist.add(node);
//        linkedlist.add(node1);
        //头插法
//        linkedlist.toucha(node);
//        linkedlist.toucha(node1);

        //尾插法
        linkedlist.weicha(node);
        linkedlist.weicha(node1);
        linkedlist.select();
    }

}

//自定义链表
class Node{
      int data;
     Node next;
    public Node(){

    }
    public Node(int data){
        this.data=data;
    }
    public Node(int data,Node next){
        this.data=data;
        this.next=next;
    }
}
//定义链表的增加
class linkedlist{
    //在这里初始化一个头节点
    Node head=new Node(0);

    public void add(Node node){
        //顶一个临时的节点用来之后遍历
        Node temp=head;
        if(head.next==null){
            head.next=node;
        }else {
            while (true){
                if(temp.next==null){
                    break;
                }
                temp=temp.next;
            }
            temp.next=node;
        }
    }
    //遍历节点
    public void select(){
        Node temp=head;
        if(head.next==null){
            System.out.println("链表为空");
        }else{
            while(true){
                if(temp.next==null){
                    break;
                }
                System.out.println(temp.next.data);
                temp=temp.next;
            }
        }
    }

    //头插法插入节点
    public void toucha(Node node){
        Node temp=head;
        if(head.next==null){
            head.next=node;
        }else {
            temp=head.next;
            node.next=temp;
            head.next=node;
        }
    }
    //尾插法插入节点
    public void weicha(Node node){
        Node temp=head;
        if(head.next==null){
            head.next=node;
        }else {
            while(true){
                if(temp.next==null){
                    temp.next=node;
                    break;
                }
                temp=temp.next;
            }
        }
    }

}













